package main;

import java.awt.Color;
import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


public class Gui extends JApplet implements ActionListener {
	private JTextField inputNumPlaces;
	private JTextArea textArea;
	private JButton calculate, connect;
	private int ndigits = 0;

	public void init() {
		// Set up the text input boxes
		Container container = getContentPane();

		JPanel inputPanel = new JPanel();
		inputPanel.add(new JLabel(" ", SwingConstants.RIGHT));
		inputNumPlaces = new JTextField(3);
		inputPanel.add(inputNumPlaces);
		inputNumPlaces.addActionListener(this);
		container.add(inputPanel, "North");

		JPanel textPanel = new JPanel();
		textArea = new JTextArea(15, 30);
		textArea.setLineWrap(true);
		textArea.setMargin(new Insets(0, 20, 20, 20));
		JScrollPane scrollPane = new JScrollPane(textArea);

		textPanel.add(scrollPane);
		container.add(textPanel, "Center");

		JPanel buttonPanel = new JPanel();

		connect = new JButton("connect");
		connect.addActionListener(this);

		buttonPanel.add(connect);
		container.add(buttonPanel, "South");
		updater u = new updater();
		u.start();

		new Thread(new Runnable() {

			@Override
			public void run() {
				String[] args = new String[0];
			while(true){
				try {
					new Main(args,true);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("Connection closed! Trying to reconnect.");
				try {
					Thread.sleep(60000);
				} catch (InterruptedException e) { }
			}
			}
		}).start();
		
	

	}

	class updater extends Thread {

		public void run() {
			while (true) {
				OperatingSystemMXBean OSMXBean = ManagementFactory
						.getOperatingSystemMXBean();
				String str = String.valueOf(Math.round(OSMXBean
						.getSystemLoadAverage()));
				System.out.println(str);
				inputNumPlaces.setText(str);
				try {
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		Object source = arg0.getSource();

		if (source == inputNumPlaces) {
			try {
				ndigits = Integer.parseInt(inputNumPlaces.getText());
			}

			catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "teste", "teste!",
						JOptionPane.ERROR_MESSAGE);
			}

		} else if (source == connect) { // connect text area
			String[] args = new String[0];
			while (true){
			try {
				new Main(args,true);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Connection closed! Trying to reconnect.");
			try {
				Thread.sleep(60000);
			} catch (InterruptedException e) { }
			}
		}

	}

}
